package cn.binux.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;

@Configuration
public class JedisPoolBootConfig {
	@Value("${spring.redis.host}")
	private String redisHost;
	@Value("${spring.redis.port}")
	private int redisPort;
	@Value("${spring.redis.pool.max-wait}")
	private Integer maxWait;
	@Value("${spring.redis.pool.max-active}")
	private Integer maxActive;
	@Value("${spring.redis.pool.max-idle}")
	private Integer maxIdle;
	@Value("${spring.redis.pool.min-idle}")
	private Integer minIdle;
	@Value("${spring.redis.timeout}")
	private int timeout;

	@Bean
	public ShardedJedisPool getShardedJedisPool() {
		// Jedis配置信息
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisPoolConfig.setMaxTotal(maxActive);
		jedisPoolConfig.setMaxIdle(maxIdle);
		jedisPoolConfig.setMinIdle(minIdle);
		jedisPoolConfig.setMaxWaitMillis(maxWait);
		JedisShardInfo shardInfo = new JedisShardInfo(redisHost, redisPort, timeout);
		List<JedisShardInfo> shardInfos = new ArrayList<>();
		shardInfos.add(shardInfo);
		return new ShardedJedisPool(jedisPoolConfig, shardInfos);
	}

	@Bean
	public JedisPool getJedisPool() {
		// Jedis配置信息
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisPoolConfig.setMaxTotal(maxActive);
		jedisPoolConfig.setMaxIdle(maxIdle);
		jedisPoolConfig.setMinIdle(minIdle);
		jedisPoolConfig.setMaxWaitMillis(maxWait);
		return new JedisPool(jedisPoolConfig, redisHost, redisPort, timeout);
	}
}
